﻿using System;
using System.Text;
using System.Web.Mvc;
using BSU.GraduateWork.BL;
using BSU.GraduateWork.Data.Model;
using BSU.GraduateWork.Data.Repositories;
using BSU.GraduateWork.Web.Models;

using System.Linq;

namespace BSU.GraduateWork.Web.Controllers
{
    [HandleError]
    public class SolutionController : Controller
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            var problem = new Problem
                                  {
                                      InputParameters = new InputParameters
                                                           {
                                                               N = 1000,
                                                               X1 = 0,
                                                               X2 = 1,
                                                               Y1 = 0,
                                                               Y2 = 1,
                                                               Z1 = 0,
                                                               Z2 = 1,
                                                               G = 0,
                                                               Epsilon = 0.0001,
                                                               H = 0.3,
                                                               Alpha = 0.00001
                                                           }
                                  };

            return View(new SolutionModel(problem));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index([Bind(Exclude = "Id")] SolutionModel model)
        {
            var method = new GridFreeMethod(model.Problem);
            method.Solve();

            //model.Problem.Solution.Points.Assign((from point
            //        in model.Problem.Solution.U
            //                                      select new Point
            //                                      {
            //                                          X = point.X,
            //                                          Y = point.Y,
            //                                          Z = point.Z,
            //                                          U = point.U,
            //                                          IsBoundary = point.IsBoundary
            //                                      }
            //            ).ToList());
            model.Problem = method.Problem;
            model.State = SolutionModel.ModelState.Solved;

            //GetVisualizationUrl(model.Problem.Solution);

            var repository = new ProblemRepository();
            repository.Save(model.Problem);

            return View(model);
        }

        private string GetVisualizationUrl(Solution solution)
        {
            var queryString = new StringBuilder();

            var points = solution.U.Where(p => p.Z == 0.1111111111111111).OrderBy(p=>p.X).Select(p => p);

            foreach (var u in points)
            {
                queryString.AppendFormat("{0:0.##}/{1:0.##}/{2:0.##}|", u.X,
                                         u.Y,
                                         u.U);
            }

            return String.Format("{0}?p={1}", "BSU.GraduateWork.VisualizerTestPage.aspx", queryString);
        }
    }
}